 

package com.zdkj.admin.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zdkj.admin.api.entity.SysDept;
import com.zdkj.admin.api.entity.SysDeptRelation;
import com.zdkj.admin.service.SysDeptRelationService;
import com.zdkj.admin.service.SysDeptService;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author cui_com
 * @since 2018-01-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dept")
@Api(value = "dept", tags = "部门管理模块")
public class SysDeptController {
	private final SysDeptRelationService relationService;
	private final SysDeptService sysDeptService;

	//部门根节点parentId
	private static final String ROOT_NODE_ID="0";

	/**
	 * 通过ID查询
	 *
	 * @param id ID
	 * @return SysDept
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return R.ok(sysDeptService.getById(id));
	}


	/**
	 * 返回树形菜单集合
	 *
	 * @return 树形菜单
	 */
	@GetMapping(value = "/tree")
	public R getTree() {
		return R.ok(sysDeptService.selectTree());
	}

	/**
	 * 添加
	 *
	 * @param sysDept 实体
	 * @return success/false
	 */
	@SysLog("添加部门")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_add')")
	public R save(@Valid @RequestBody SysDept sysDept) {
		String deptName = sysDept.getName();
		QueryWrapper queryWrapper=new QueryWrapper();       
        queryWrapper.eq("name",deptName);  
        int nameCount=sysDeptService.count(queryWrapper);
        if(nameCount>0){
            return R.failed(deptName+" 数据已存在！");
        }

		return R.ok(sysDeptService.saveDept(sysDept));
	}

	/**
	 * 删除
	 *
	 * @param id ID
	 * @return success/false
	 */
	@SysLog("删除部门")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_dept_del')")
	public R removeById(@PathVariable Integer id) {
		return R.ok(sysDeptService.removeDeptById(id));
	}

	/**
	 * 编辑
	 *
	 * @param sysDept 实体
	 * @return success/false
	 */
	@SysLog("编辑部门")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_dept_edit')")
	public R update(@Valid @RequestBody SysDept sysDept) {

		String deptName = sysDept.getName();
		QueryWrapper queryWrapper=new QueryWrapper();       
        queryWrapper.eq("name",deptName);  
		queryWrapper.ne("dept_id",sysDept.getDeptId());  
        int nameCount=sysDeptService.count(queryWrapper);
        if(nameCount>0){
            return R.failed(deptName+" 数据已存在！");
        }
		sysDept.setUpdateTime(LocalDateTime.now());
		return R.ok(sysDeptService.updateDeptById(sysDept));
	}

	/**
	 * 查收子级列表
	 *
	 * @return 返回子级
	 */
	@GetMapping(value = "/getDescendantList/{deptId}")
	public R getDescendantList(@PathVariable Integer deptId) {
		return R.ok(relationService.list(Wrappers.<SysDeptRelation>lambdaQuery()
				.eq(SysDeptRelation::getAncestor, deptId)));
	}

	/**
	 * 返回部门字典
	 *
	 * @return 返回子级
	 */
	@GetMapping(value = "/getBlackDept")
	public R getBlackDept() {
		return R.ok(sysDeptService.list());
	}
	/**
	 * 返回部门根节点
	 *
	 * @return 返回根节点
	 */
	@GetMapping(value = "/getRootNode")
	public R getRootNode() {
		SysDept sysDept = new SysDept();
        return R.ok(sysDeptService.list(Wrappers.lambdaQuery(sysDept).eq(SysDept::getParentId,ROOT_NODE_ID)));
	}
    /**
     * 返回部门根节点的一级子节点
     *
     * @return 返回子级
     */
    @GetMapping(value = "/getChildNode/{name}")
    public R getChildNode(@PathVariable String name) {
        SysDept sysDept = new SysDept();
        SysDept one = sysDeptService.getOne(Wrappers.lambdaQuery(sysDept).eq(SysDept::getName, name).last("LIMIT 1"));
        return R.ok(sysDeptService.list(Wrappers.lambdaQuery(sysDept).eq(SysDept::getParentId,one.getDeptId())));
    }
}
